package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.utils.DateUtil;
import cc.rengu.redp.common.utils.SortUtil;
import cc.rengu.redp.bizimpl.upmp.dao.IndustryMapInfoMapper;
import cc.rengu.redp.bizimpl.upmp.domain.IndustryMapInfo;
import cc.rengu.redp.bizimpl.upmp.service.IndustryMapInfoService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author LiuYuLiang.LL
 */
@Service
@DS("upmp")
public class IndustryMapInfoServiceImpl extends ServiceImpl<IndustryMapInfoMapper, IndustryMapInfo> implements IndustryMapInfoService {
    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Override
    public IPage<IndustryMapInfo> findByPage(QueryRequest request, IndustryMapInfo info) {
        try {
            LambdaQueryWrapper<IndustryMapInfo> queryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(info.getIndustryType())) {
                queryWrapper.like(IndustryMapInfo::getIndustryType, info.getIndustryType());
            }
            if (StringUtils.isNotBlank(info.getDestCatalogCd())) {
                queryWrapper.like(IndustryMapInfo::getDestCatalogCd, info.getDestCatalogCd());
            }
            if (StringUtils.isNotBlank(info.getDestChannel())) {
                queryWrapper.like(IndustryMapInfo::getDestChannel, info.getDestChannel());
            }
            queryWrapper.orderByDesc(IndustryMapInfo::getCreateTime);
            Page<IndustryMapInfo> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            return this.baseMapper.findByPage(page, queryWrapper);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }

    @Override
    @Transactional
    public void create(IndustryMapInfo entity) {
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String datetime= DateUtil.getDateFormat(new Date(),DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN);
        entity.setStatus(BusinessContainer.data_sta_01);
        entity.setAuditStatus(BusinessContainer.status_02);
        entity.setOprId(user.getId());
        entity.setCrtOrgId(user.getOrgCode());
        entity.setLastOprId(user.getId());
        entity.setCreateTime(datetime);
        entity.setLastUpdateTime(datetime);
        this.baseMapper.insert(entity);
    }

    @Override
    @Transactional
    public void update(IndustryMapInfo entity) {
        // 获取当前登录用户信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        entity.setAuditStatus(BusinessContainer.status_42);
        entity.setLastOprId(user.getId());
        entity.setLastUpdateTime(DateUtil.getDateFormat(new Date(),DateUtil.FULL_TIME_WITH_MILL_SPLIT_PATTERN));
        this.baseMapper.updateById(entity);
    }

    @Override
    @Transactional
    public void cancel(String id) {
        // 注销
        IndustryMapInfo entity = this.getById(id);
        entity.setStatus(BusinessContainer.data_sta_03);
        entity.setAuditStatus(BusinessContainer.status_12);
        this.baseMapper.updateById(entity);
    }

    @Override
    @Transactional
    public void delete(String[] ids) {
        List<String> list = Arrays.asList(ids);
        this.baseMapper.deleteBatchIds(list);
    }

    @Override
    public IPage<HashMap<String, Object>> destCatalogCdSelect2List(QueryRequest request, String q) {
        try {
            Page<HashMap<String, Object>> page = new Page<>();
            SortUtil.handlePageSort(request, page, true);
            /*if (null == q) {
                return page ;
            }*/
            q = q.replaceAll("&amp;", "&");
            String[] param = q.split("&");
//            StringBuilder sql = new StringBuilder();
            HashMap<String, Object> paramMap = new HashMap<>();
            String value=null;
            switch (param[0]) {
//            case "CUPS":
//                sql.append("select a.mchnt_type as id,a.mcc_desc as name from t_b_industry_info i inner join t_b_industry_map_info m on i.indus_cd=m.indus_cd inner join (select g.grp_no,g.grp_desc,t.mchnt_type,t.mcc_desc,t.card_org_flag from tbl_mcc_info t inner join tbl_mer_grp_info g on t.grp_no=g.grp_no and t.card_org_flag='01' and t.grp_no<>'00')a on m.dest_channel='CUPS' AND m.dest_catalog_cd = a.mchnt_type ");
//                if (param.length > 1)
//                    sql.append("where a.mchnt_type like '").append(param[1]).append("%' or ").append("a.mcc_desc like '").append(param[1]).append("%'");
//                break;
//            case "UPQR":
//                sql.append("select a.mchnt_type as id,a.mcc_desc as name  from tbl_industry_info i inner join tbl_industry_map m on i.indus_cd=m.indus_cd inner join (select g.grp_no,g.grp_desc,t.mchnt_type,t.mcc_desc,t.card_org_flag from tbl_mcc_info t inner join tbl_mer_grp_info g on t.grp_no=g.grp_no and t.card_org_flag='06' and t.grp_no<>'00')a on m.dest_channel='UPQR' AND m.dest_catalog_cd = a.mchnt_type ");
//                if (param.length > 1)
//                    sql.append("where a.mchnt_type like '").append(param[1]).append("%' or ").append("a.mcc_desc like '").append(param[1]).append("%'");
//                break;
                case "WXZF":
                    if (param.length > 1) {
                       value= param[1];
                    }
                    return this.baseMapper.selectWXZFList(page, value);
                case "ALIP":
                    if (param.length > 1) {
                         value= param[1];
                    }
                    return this.baseMapper.selectALIPList(page, value);
            }
            return page;
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }
    @Override
    public IndustryMapInfo findIndustryMap(String indusCd, String destChannel, String publicPrivateFlag) {
        List<IndustryMapInfo> list = getByUk(indusCd, destChannel,publicPrivateFlag);
        if(CollectionUtils.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }
    private  List<IndustryMapInfo> getByUk(String indusCd, String destChannel,String publicPrivateFlag) {
        LambdaQueryWrapper<IndustryMapInfo> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(IndustryMapInfo::getIndustryType,indusCd);
        wrapper.eq(IndustryMapInfo::getDestChannel,destChannel);
        wrapper.eq(IndustryMapInfo::getPublicPrivateFlag,publicPrivateFlag);
        return this.list(wrapper);
    }

}
